<template>
  <div class="category-list">
    <el-card>
      <!-- 工具栏 -->
      <div class="toolbar">
        <el-button type="primary" @click="handleAdd">新增分类</el-button>
      </div>

      <!-- 数据表格 -->
      <el-table
        v-loading="loading"
        :data="tableData"
        row-key="id"
        border
        default-expand-all
      >
        <el-table-column prop="name" label="分类名称" />
        <el-table-column prop="code" label="分类编码" width="120" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleAdd(row)">添加子分类</el-button>
            <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button link type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '新增分类' : '编辑分类'"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="分类编码" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="form.description"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance } from 'element-plus'

// 表格数据
const loading = ref(false)
const tableData = ref([
  {
    id: 1,
    name: '处方药',
    code: 'RX',
    description: '需要医生处方的药品',
    children: [
      {
        id: 11,
        name: '抗生素类',
        code: 'RX-KSS',
        description: '抗生素类药品'
      },
      {
        id: 12,
        name: '心血管类',
        code: 'RX-XXG',
        description: '心血管类药品'
      }
    ]
  },
  {
    id: 2,
    name: '非处方药',
    code: 'OTC',
    description: '非处方药品',
    children: [
      {
        id: 21,
        name: '感冒用药',
        code: 'OTC-GM',
        description: '感冒类药品'
      },
      {
        id: 22,
        name: '维生素类',
        code: 'OTC-WSS',
        description: '维生素类药品'
      }
    ]
  }
])

// 对话框
const dialogVisible = ref(false)
const dialogType = ref('add')
const formRef = ref<FormInstance>()
const form = reactive({
  name: '',
  code: '',
  description: '',
  parentId: null as number | null
})

// 表单验证规则
const rules = {
  name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
  code: [{ required: true, message: '请输入分类编码', trigger: 'blur' }]
}

// 新增
const handleAdd = (row?: any) => {
  dialogType.value = 'add'
  dialogVisible.value = true
  // 重置表单
  Object.keys(form).forEach(key => {
    form[key] = ''
  })
  // 如果是添加子分类，设置父ID
  if (row) {
    form.parentId = row.id
  }
}

// 编辑
const handleEdit = (row: any) => {
  dialogType.value = 'edit'
  dialogVisible.value = true
  // 填充表单
  Object.keys(form).forEach(key => {
    form[key] = row[key]
  })
}

// 删除
const handleDelete = (row: any) => {
  ElMessageBox.confirm(
    `确定要删除分类 ${row.name} 吗？${row.children?.length ? '该分类下的子分类也会被删除！' : ''}`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // TODO: 实现删除逻辑
    ElMessage.success('删除成功')
  })
}

// 表单提交
const handleSubmit = async () => {
  if (!formRef.value) return
  await formRef.value.validate((valid) => {
    if (valid) {
      // TODO: 实现提交逻辑
      ElMessage.success(dialogType.value === 'add' ? '添加成功' : '更新成功')
      dialogVisible.value = false
    }
  })
}
</script>

<style scoped>
.category-list {
  padding: 20px;
}
.toolbar {
  margin-bottom: 20px;
}
</style> 